var searchType, searchText;
var db;



function connectDB(type, string) {
	searchType = type;
	searchText = string;
	db = window.openDatabase("JapaneseBasic", "1.0", "Japanese Database",
			500000);
	db.transaction(searchProcess, errorCB);
}

function errorCB(err) {
	alert("Error processing SQL: " + err.code);
}

function successCB() {
	db.transaction(searchProcess, errorCB);
}

function searchProcess() {
	/*
	 * searchType = type; searchText = string; connectDB();
	 */
	switch (searchType) {
	case 'kanji':
		db.transaction(searchKanji, errorCB);
		break;
	case 'vocabulary':
		db.transaction(searchVocabulary, errorCB);
		break;
	case 'grammar':
		db.transaction(searchGrammar, errorCB);
	}
}

// search vocabulary
function searchVocabulary(tx) {
	var sql = "SELECT v_id,v_word,v_hiragana,v_mean_kanji,v_mean_en,v_mean_vi,v_lesson_id,v_note,v_favourite,v_dataversion_id, le_title "
			+ " FROM vocabulary "
			+ " INNER JOIN lesson"
			+ " ON vocabulary.v_lesson_id = lesson.le_id"
			+ " WHERE v_word LIKE '%"
			+ searchText
			+ "%' "
			+ " OR v_hiragana LIKE '%"
			+ searchText
			+ "%' "
			+ " OR v_mean_kanji LIKE '%"
			+ searchText
			+ "%' "
			+ " OR v_mean_en LIKE '%"
			+ searchText
			+ "%' "
			+ " OR v_mean_vi LIKE '%"
			+ searchText
			+ "%' "
			+ " ORDER BY v_id ASC ";
	tx.executeSql(sql, [], showVocabularyResult);
}

function showVocabularyResult(tx, results) {
	var len = results.rows.length;
	if (len == 0) {
		$('#vocabulary-list')
				.append(
						'<div style="margin: 20px; 20px;">Không tìm thấy kết quả</div>');
	} else {
		for (var i = 0; i < len; i++) {
			var data = results.rows.item(i);
			$('#vocabulary-list')
					.append(
							'<li style="height: 110px;" class="ui-li-static ui-body-inherit ui-first-child ui-last-child"><fieldset class="ui-list-a"> <div class="ui-block-a" style="position: absolute; top: 7px; right: 60px; left: 45px;">'
									+ '<h5 class="kanji" style="float: left;display:block;">'
									+ data.v_word
									+ '</h5> '
									+ '<h3 class="mean_kanji" style="float: right; font-size: 12px; margin-top: 15px;text-transform:uppercase;display:block;"><span>&nbsp;</span><span>'
									+ data.v_mean_kanji
									+ '</span></h3> '
									+ '<p class="hiragana" style="float: left; clear: both; margin-left: -20px;display:block;"><span>&nbsp;</span><span>'
									+ data.v_hiragana
									+ '</span></p> '
									+ '<p class="mean_en" style="float: left; clear: both; margin-left: -20px;display:block;"><span>&nbsp;</span><span>'
									+ data.v_mean_en
									+ '</span></p> '
									+ '<p class="mean_vi" style="float: left; clear: both; margin-left: -20px;display:block;"><span>&nbsp;</span><span>'
									+ data.v_mean_vi
									+ '</span></p>'
									+ '<p class="lesson" style="float: left; clear: both; margin-left: -20px;display:block;"><span>&nbsp;</span><span>'
									+ data.le_title
									+ '</span></p>'
									+ '</div><div class="ui-block-b" style="position: absolute; top: -2px; right: 5px;"><div data-role="controlgroup" data-type="vertical" style="width: 30px;" class="ui-controlgroup ui-controlgroup-vertical ui-corner-all"><div class="ui-controlgroup-controls "> '
									+ '</div></div></fieldset></li>');
		}
	}
}

// search kanji
function searchKanji(tx) {
	var sql = "SELECT k_id, k_kanji ,k_mean_kanji, k_mean_en ,k_mean_vi "
			+ " FROM kanji " + " WHERE k_kanji LIKE '%" + searchText
			+ "%' OR k_mean_kanji LIKE '%" + searchText
			+ "%' OR k_mean_en LIKE '%" + searchText
			+ "%' OR k_mean_vi LIKE '%" + searchText + "%' "
			+ " ORDER BY k_id ASC ";
	tx.executeSql(sql, [], showKanjiResult);
}

function showKanjiResult(tx, results) {
	var len = results.rows.length;
	if (len == 0) {
		$('#kanjiListView')
				.append(
						'<div style="margin: 20px; 20px;">Không tìm thấy kết quả</div>');
	} else {
		for (var i = 0; i < len; i++) {
			var data = results.rows.item(i);
			$('#kanjiListView')
					.append(
							'<li class="ui-first-child"><a  href="kanji_detail.html?id='
									+ data.k_id
									+ '" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r"> '
									+ '<span id="wrap">'
									+ '<span class="kanji" style="font-weight:bold;font-size: 18px;margin-left:-50px;">'
									+ data.k_kanji
									+ '</span>'
									+ '<span class="mean_kanji" style="text-transform:uppercase;font-weight:bold;font-size: 16px;">'
									+ data.k_mean_kanji + '</span>'
									+ '<span class="mean_vi">' + data.k_mean_vi
									+ '</span> ' + '<span class="mean_en">'
									+ data.k_mean_en + '</span>'
									+ '</span></a></li>');
		}
	}
}

// search grammar
function searchGrammar(tx) {
	var sql = "SELECT g_id, g_title, g_mean, g_lesson_id, le_id, le_title "
			+ " FROM grammar " + " INNER JOIN lesson"
			+ " ON grammar.g_lesson_id = lesson.le_id"
			+ " WHERE g_title LIKE '%" + searchText + "%' "
			+ " OR g_mean LIKE '%" + searchText + "%' " + " ORDER BY g_id ASC ";
	tx.executeSql(sql, [], showGrammarResult);
}

function showGrammarResult(tx, results) {
	var len = results.rows.length;
	if (len == 0) {
		$('#grammar-list')
				.append(
						'<div style="margin: 20px; 20px;">Không tìm thấy kết quả</div>');
	} else {
		for (var i = 0; i < len; i++) {
			var data = results.rows.item(i);
			$('#grammar-list')
					.append(
							'<li class="ui-first-child"><a href="grammar_detail.html?leid='
									+ data.g_lesson_id
									+ '&id='
									+ data.g_id
									+ '" data-ajax="false" data-direction="reverse" class="ui-btn ui-btn-icon-right ui-icon-carat-r" style="font-weight:normal;font-family:Times New Roman, Times, serif;"> '
									+ data.g_title + '</a></li>');
		}
	}
}
